import get from '@dfeidao/atom-web/local/get';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
// import success from '@dfeidao/atom-web/msgbox/success';
interface Aa {
	role_no: string;
	role_allot_id: string;
	group_id: string;
	role_type_id: string;
	group: string;
}
export default async function a003(fd: IFeidaoAiBrowserComponent, args: Event) {
	//  显示新增弹窗
	const node = args.currentTarget as HTMLInputElement;
	const component_no = node.getAttribute('data-component');
	const productid = get(fd, 'productid');
	const event_no = node.getAttribute('data-event') || 'ec-001';
	const node_name = node.name;
	const c_boxs = fd.data.node.querySelectorAll('[data-feidao-name="list_checkbox"]:checked');
	const ids = Array.from(c_boxs).map((item) => {
		return item.getAttribute('data-feidao-id4');
	});
	// if (c_boxs.length > 1 || c_boxs.length === 0) {
	// 	error('请选择一条数据');
	// 	return;
	// }
	const role = get(fd, 'role') as Aa;
	const role_no = role.role_no;
	const role_type_id = role.role_type_id;
	const role_allot_id = get(fd, 'role_allot_id') as string;
	const allot_group = get(fd, 'allot_group') as Aa;
	const shezhi = get(fd, 'shezhi') as Aa;
	const ff = get(fd, 'ff');
	const b_component_no = 'zj-000004';
	// debugger;
	if (node_name === 'zeng') {
		fire(component_no, event_no, role_no, ff, b_component_no);
	} else if (!role_allot_id) {
		error('请选择权限');
		return;
	} else if (!allot_group) {
		error('请选择条件组');
		return;
	} else {
		if (node_name === 'xiu') {
			fire(component_no, event_no, { allot_group, role_allot_id, ids, b_component_no });
		} else if (node_name === 'quan') {
			const obj = {
				group_id: allot_group.group_id,
				role_type_id,
				group: allot_group.group,
				role_allot_id
			} as Aa;
			const query = { productid, role_allot_id, group_id: allot_group.group_id };
			show_loading();
			const count = await nodejs('roler/zj-000004/s004', query);
			hide_loading();
			if (count !== 0) {
				error('合并后的条件组不能新增权限');
				return;
			}
			fire(component_no, event_no, obj, b_component_no);
		} else {
			if (!shezhi) {
				error('请选择条件设置');
				return;
			}
			// debugger;
			fire(component_no, event_no, shezhi, productid, b_component_no);
		}
	}
}
